package com.citusdata.elven.linearRoad;

import java.lang.Math;


// Helper class to partition data on expressway, direction and segment.
class FullSegment {
  private static final int LAST_EAST_SEGMENT = 99;
  private static final int LAST_WEST_SEGMENT = 0;

  private final int expressway;
  private final int direction;
  private final int segment;

  public FullSegment(int xway, int dir, int seg) {
    expressway = xway;
    direction = dir;
    segment = seg;
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof FullSegment)) {
      return false;
    }
    FullSegment seg = (FullSegment) obj;
    return (seg.expressway == expressway) && (seg.direction == direction) &&
           (seg.segment == segment);
  }

  public int hashCode() {
    int result = 17;
    result = 37*result + expressway;
    result = 37*result + direction;
    result = 37*result + segment;
    return result;
  }

  public int getSegment() {
    return segment;
  }

  public static FullSegment downstreamSegment(FullSegment seg, int count) {
    int downstream = 0;
    if (seg.direction == 0) {
      // East bound
      downstream = Math.min(seg.segment+count, LAST_EAST_SEGMENT);
    } else {
      downstream = Math.max(seg.segment-count, LAST_WEST_SEGMENT);
    }

    return new FullSegment(seg.expressway, seg.direction, downstream);
  }
}
